#include "Character_Ogre.h"

#include "../../RPGAction.h"
#include "../../RPGItem.h"

#include "../../Acciones/Action_Punch.h"
#include "../../Acciones/Action_Distance.h"
#include "../../Acciones/Action_Walk.h"
#include "../../Acciones/Action_Idle.h"
#include "../../Acciones/Action_Died.h"
#include "../../Acciones/Action_Sleeping.h"
//------------------------------------------------------------------------------------
Character_Ogre::Character_Ogre()
:
RPGCharacter(),
m_fCurrentIATime(0),
m_bMoveUp(false)
{
	strcpy(m_pcNormalINI,"./data/ogre/ogre.spr");
	strcpy(m_pcBeltINI,"./data/ogre/ogre.spr");
	// cargo el sprite del personaje
	LoadIni(m_pcNormalINI);

	// le seteo la velocidad de movimiento
	SetSpeed(0.1f);

	// accion "Idle" (cuando el personaje no hace nada)
	AddNewAction(new Action_Idle());

	// accion "Punch" (cuando el personaje pega)
	AddNewAction(new Action_Punch());

	// accion "Distance" (cuando el personaje pega a distancia)
	AddNewAction(new Action_Distance());

	// accion "Walk" (cuando el personaje camina)
	AddNewAction(new Action_Walk());

	AddNewAction(new Action_Died());
	AddNewAction(new Action_Sleeping());
	// se setea por default la accion "Idle"
	Idle();

	// setea los atributos por default
	SetMaxEnergy(100);					// setea la energia actual
	SetEnergy(GetMaxEnergy());			// setea la energia maxima

}
//------------------------------------------------------------------------------------
Character_Ogre::~Character_Ogre()
{

}
//------------------------------------------------------------------------------------
void Character_Ogre::OnActionFinished(char *pszName)
{
	RPGCharacter::OnActionFinished(pszName);

	// pasamos a las acciones correspondientes 
	//		segun si el personaje es el principal o no
	if((m_pkPlayer1 == this) || (m_pkPlayer2 == this))
	{
		// cuanto termina la accion de golpear, que se quede quieto
		if(strcmp(pszName, "Punch") == 0)
			Idle();
		
		// cuanto termina la accion de caminar, que se quede quieto
		else if(strcmp(pszName, "Walk") == 0)
			Idle();

		else if(strcmp(pszName, "Distance") == 0)
			Idle();

	}
	// si este personaje no es el principal, entonces actua la inteligencia artificial
	else
	{
		if(strcmp(pszName, "Punch") == 0)
			Idle();

		else if(strcmp(pszName, "Distance") == 0)
			Idle();
		
		else if(strcmp(pszName, "Sleeping") == 0)
			Idle();
	}
}
//------------------------------------------------------------------------------------
void Character_Ogre::Draw()
{
	RPGCharacter::Draw();
}
//------------------------------------------------------------------------------------
void Character_Ogre::Update(float fTimeBetweenFrames)
{
	RPGCharacter::Update(fTimeBetweenFrames);
}
//------------------------------------------------------------------------------------
void Character_Ogre::UpdateIA(float fTimeBetweenFrames)
{
	RPGCharacter::UpdateIA(fTimeBetweenFrames);

	m_fCurrentIATime += fTimeBetweenFrames;

	if((strcmp(m_pCurrentAction->pszName, "Died") != 0) && (strcmp(m_pCurrentAction->pszName, "Sleeping") != 0))
	{
		//cout << m_pkColliding << endl;
		if(m_pkColliding)
		{
			if (m_pkColliding->GetEnergy()>0)
				Hit();
			else
			{
				m_pkColliding->Died();
				Action1();
			}
		}
		else if (m_fEnergy <= 0)
		{
			Died();
		}
		else if(strcmp(m_pCurrentAction->pszName, "Walk") == 0)
		{
			if(m_fCurrentIATime >= 2000)
			{
				m_fCurrentIATime = 0.0f;
				
				if(m_bMoveUp)
					Move(90.0f);
				else
					Move(270.0f);

				m_bMoveUp = !m_bMoveUp;
			}
		}
		// si el personaje esta quieto, hacer que camine
		else if(strcmp(m_pCurrentAction->pszName, "Idle") == 0)
		{
			Move(270.0f);
			m_bMoveUp = true;
		}
	}
}
//------------------------------------------------------------------------------------

// llamado a las acciones

void Character_Ogre::Idle()
{
	SetMoving(false);
	SetCurrentAction("Idle");
}
void Character_Ogre::Hit()
{
	SetMoving(false);
	SetCurrentAction("Punch");
}
void Character_Ogre::Action1()
{
	SetMoving(false);
	SetCurrentAction("Distance");
}
void Character_Ogre::Action2()
{
	SetCurrentAction("Sleeping");
	SetMoving(false);
}
void Character_Ogre::Died()
{
	SetMoving(false);
	SetCurrentAction("Died");
}
void Character_Ogre::Move(float fDirection)
{
	if(fDirection == 90.0f){
		if(m_bRotated){
			FlipHorizontal();
			m_bRotated = false;
		}
	}
	else if(fDirection == 270.0f){
		if(!m_bRotated){
			FlipHorizontal();
			m_bRotated = true;
		}
	}

	SetCurrentAction("Walk");

	SetMovDirection(fDirection);
	SetMoving(true);



}